﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;

namespace HobSoft.SoccerBet2014.Domain.Models.Site
{
    using Core;
    using KeyHelper;
    [Serializable]
    [DataContract]
    public class SiteContext : IModel
    {

        public SiteContext(byte bookerId, string account, string modelKey, string ckey)
        {
            BookerID = bookerId;
            Account = account;
            ModelKey = modelKey;
            ContextKey = ckey;
            LastUpdate = DateTime.Now;
            _autoDeser = false;
        }

        protected readonly bool _autoDeser = true;

        [PrimaryKey]
        [DataMember(IsRequired = true)]
        public string Key
        {
            get
            {
                return KeyGenerateHelper.Create<SiteContext>(this);
            }
            set { }
        }
        [Key]
        [DataMember(IsRequired = true)]
        public byte BookerID { get; set; }

        [Key]
        [Required]
        [MaxLength(50)]
        [MinLength(2)]
        [DataMember(IsRequired = true)]
        public string Account { get; set; }

        [Required]
        [MaxLength(50)]
        [MinLength(2)]
        [DataMember(IsRequired = true)]
        public string ModelKey { get; set; }

        [Required]
        [MaxLength(50)]
        [MinLength(2)]
        [DataMember(IsRequired = true)]
        public string ContextKey { get; set; }

        [Required]
        [MaxLength(800)]
        [DataMember(IsRequired = true)]
        public string ContextValue { get; set; }
        [DataMember]
        public DateTime LastUpdate { get; set; }

        public void RecoverContextValue(string value)
        {
            ContextValue = value;
            if (OnRecoverContextValue != null)
                OnRecoverContextValue(value);
        }

        [field:NonSerialized]
        public event Action<string> OnRecoverContextValue;

        public bool Check(byte bookerId, string account)
        {
            return (BookerID == bookerId && Account == account);
        }
    }
}
